package com.game.service.windows;
import com.game.service.define.RoomConfig;
import com.game.service.game.redblack.RedBlackTable;
import com.game.service.socket.SocketServer;
import com.google.gson.Gson;
import javax.swing.*;
import java.awt.event.ActionEvent;


public class Client extends JFrame {
    private static boolean CLOSE;
    JLabel label = new JLabel("提示文本");
    JButton button = new JButton("关闭程序");
    JButton buttonAdd = new JButton("查看盈利");
    JButton buttonTest = new JButton("测试下注");
    JButton buttonConfig = new JButton("查看配置");
    JButton buttonClear = new JButton("清空日志");

    public Client() {
        setTitle(RoomConfig.SERVER_NAME); // 设置窗口标题
        setSize(400, 300); // 设置窗口大小（宽度 x 高度）
        setResizable(false); // 窗口大小不可更改
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); // 设置窗口关闭操作，默认为隐藏或无操作

        setLocationRelativeTo(null); // 窗口居中显示

        JPanel panel = new JPanel();

        button.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                close();
            }
        });


        buttonAdd.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                SocketServer.getKernel().printSystemRunInfo();
            }
        });
        buttonTest.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                AddTest();
            }
        });
        buttonConfig.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                UpdateConfig();
            }
        });
        buttonClear.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                clearTextArea();
            }
        });

        panel.add(button);
        panel.add(buttonAdd);
        panel.add(buttonTest);
        panel.add(buttonConfig);
        panel.add(buttonClear);
        panel.add(label);
        this.getContentPane().add(panel);
        setVisible(true); // 显示窗口

    }


    public static boolean isClose() {
        return CLOSE;
    }

    public static void setClose() {
        CLOSE = true;
    }

    public void close() {
        int option = JOptionPane.showConfirmDialog(null, "你确定要退出吗？", "退出程序", JOptionPane.YES_NO_OPTION);

        if (option == JOptionPane.YES_OPTION) {
            setClose();
            label.setText("管理员点击了确定,立即踢掉所有玩家,当局结算后关闭服务器程序");
        } else if (option == JOptionPane.NO_OPTION) {
            label.setText("管理员取消了操作");
        }
    }

    public void AddTest() {
        if (RoomConfig.isRelease) {
            return;
        }
        int option = JOptionPane.showConfirmDialog(null, "你确定添加测试数据吗？", "添加测试数据", JOptionPane.YES_NO_OPTION);

        if (option == JOptionPane.YES_OPTION) {
            RedBlackTable.testBet();
        }
    }

    public void UpdateConfig() {

        String log = String.format("当前程序内加载的配置:%s", new Gson().toJson(RedBlackTable.getConfig()));//打印当前的配置
        LogWindows.getLogWindows().printToTextArea(log,Client.class);
        int option = JOptionPane.showConfirmDialog(null, "你确定立即更新配置吗？", "不停服,更新配置", JOptionPane.YES_NO_OPTION);
        if (option == JOptionPane.YES_OPTION) {
            RedBlackTable.initGameConfig();//点击确定,则在线更新最新配置
        }
    }

    public void clearTextArea() {

        int option = JOptionPane.showConfirmDialog(null, "你确定清空当前显示日志？", "清空", JOptionPane.YES_NO_OPTION);

        if (option == JOptionPane.YES_OPTION) {
           LogWindows.getLogWindows().clearTextArea();
        }
    }


}